<?php
include '../template.php';

include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include '../util/dbutil.php';
include '../util/pageutil.php';

?>
	
<?php

//function getimg($ord,$ad,$v,$get) {
//    $arr = explode(" ",$ord);
//    if (strcasecmp($arr[0],$v)!=0 || strcasecmp($arr[1],$ad)!=0) {
//        if (strcasecmp($ad,"asc")==0) {
//                $img = "<img src=\"../images/up_gray.gif\" border=\"0\" width=\"13\" height=\"12\" />";
//        } else {
//                $img = "<img src=\"../images/down_gray.gif\" border=\"0\" width=\"13\" height=\"12\" />";
//        }
//
//        return "<a href=\"$self?$get&ord=$v $ad\" title=\"click to sort\" style=\"color:#FFFFFF\">$img</a>";
//    } else {
//        if (strcasecmp($ad,"asc")==0) {
//                $img = "<img src=\"../images/up.gif\" border=\"0\" width=\"13\" height=\"12\" />";
//        } else {
//                $img = "<img src=\"../images/down.gif\" border=\"0\" width=\"13\" height=\"12\" />";
//        }
//        return $img;
//    }
//}

//if (isset($_POST['search'])) { // first
//	$acc = trim($_POST['acc']);
//        $idot = strpos($acc,".");
//        if ($idot) {
//                $acc = substr($acc, 0, $idot);
//        }
//
//	$desc = trim($_POST['desc']);
//	$org = $_POST['org'];
//        $ref = $_POST['ref'];
//
////        $ord = "score desc";
//
//	// by default we show first page
//	$pageNum = 1;
//} elseif (isset($_POST['browse'])) {
//        $org = $_POST['org'];
//        $ref = $_POST['ref'];
////        $ord = "score desc";
//        $pageNum = 1;
//} elseif (isset($_GET['page'])) { //if(isset($_GET['page']))
	$pageNum = $_GET['page'];
        if (!isset($pageNum))
            $pageNum = 1;

	$acc = $_GET['acc'];
	$desc = $_GET['desc'];
        $ref = $_GET['ref'];
	$org = $_GET['org'];
        $count = $_GET['count'];
//        $ord = $_GET['ord'];
//} else {
//        die('Wrong entry!');
//}

//if (strlen($acc)==0&&strlen($desc)==0) {
//	echo "&nbsp;<br /> &nbsp;<br />";
//	echo "Please search by protein accession and/or protein description!";
//} else {
	$where = "";
	$get = "";
        
        $first = true; // first filter
	
	if (strlen($acc)!=0) {
                $acc = strtolower($acc);
		$where = "(id IN (SELECT p3dbId FROM idMapping WHERE lower(otherId) LIKE '%$acc%'))";
		$get = "acc=$acc";
                $first = false;
	}
	
	if (strlen($desc)!=0) {
		$desc = strtolower($desc);
		if ($first) {
			$first = false;
		} else {
			$where = $where." AND ";
			$get = $get."&";
		}
                $where = $where."lower(annotation) LIKE '%$desc%'";
                $get = $get."desc=$desc";
	}
        
	
	if (!empty($org)) { // some specific org was selected
            if ($first) {
                $first = false;
            } else {
                $where = $where." AND ";
                $get = $get."&";
            }
            $where = $where."organism='$org'";
            $get = $get."org=$org";
//            setcookie("p3db-organism", $org);
	} 
        	
	if (!empty($ref)) { // some specific ref was selected
            if ($first) {
                $first = false;
            } else {
                $where = $where." AND ";
                $get = $get."&";
            }
            $where = $where."EXISTS (SELECT * from proteinDataSourceRelation WHERE proteinDataSourceRelation.protein=protein.id AND proteinDataSourceRelation.datasource='$ref')";
            $get = $get."ref=$ref";
//            setcookie("p3db-datasource", $ref);
//            print_r($_COOKIE);
	}

        $self = $_SERVER['PHP_SELF'];

        // how many rows to show per page
        $rowsPerPage = 20;

        // counting the offset
        $offset = ($pageNum - 1) * $rowsPerPage;

        // get count
        if (!isset($count)) {
            if ($first) {
                $query = "SELECT count(*) FROM protein;";
            } else {
                $query = "SELECT count(*) FROM protein WHERE $where;";
            }
            $result = mysql_query($query) or die("Error, query failed: $query");
            while ($row = mysql_fetch_array($result)) {
                    $count = $row[0];
            }
        }
        $get .= "&count=$count";

        // query database
        if ($first) {
                $query  = "SELECT id, annotation, latin ".
                          "FROM protein,organism WHERE protein.organism=organism.label ".
//                          "ORDER BY $ord ".
                          "LIMIT $offset, $rowsPerPage;";
        } else {
                $query  = "SELECT id, annotation, latin ".
                          "FROM protein,organism WHERE $where AND protein.organism=organism.label ".
//                          "ORDER BY $ord ".
                          "LIMIT $offset, $rowsPerPage;";
        }
//        echo $query;
        $result = mysql_query($query) or die("Error, query failed: $query");

        if ($count==1) {
            $row = mysql_fetch_array($result, MYSQL_ASSOC);
            $protId = $row['id'];
            // redirect
            echo "<html>";
            echo "<head>";
            echo "<meta http-equiv=\"REFRESH\" content=\"0;url=../data/protein.php?id=$protId\">";
            echo "</head>";
            echo "</html>";
        }

        getTopWithJS("..");
        echo "<p><font size=\"4\">Totally found $count phosphoproteins:</font></p>";

        // write the data
        echo "<table border=\"1\" width=\"%100\">";
        echo "<tr valign=\"bottom\"><td><b>Accession</b></td>".
                  "<td align=\"center\"><b>Description</b></td>".
                  "<td align=\"center\"><b>Organism</b></td>".
//                  "<td align=\"center\">".
//                          "<b>Reference</b><br/>".
//                          getimg($ord,"asc","reference","page=$pageNum&$get").getimg($ord,"desc","reference","page=$pageNum&$get")."</td>".
                  "</tr>";

        while($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
                $protId = $row['id'];
                $xref = implode('<br/>', getXref($protId));
//                echo $acc;
                echo "<tr><td><a href=\"../data/protein.php?id=$protId";
                if (!empty($ref)) {
                    echo "&ref=$ref";
                }
                echo "\">$xref</a></td>";

                echo "<td>{$row['annotation']}</td>";
                echo "<td align=\"center\"><i>{$row['latin']}</i></td></tr>";
        }
        echo "</table>";

        // how many pages we have when using paging?
        $maxPage = ceil($count/$rowsPerPage);

        printPageNav($maxPage,"$self?$get&",$pageNum);

        //echo "<table width=\"100%\"><tr><td align=\"right\"><form method=\"post\" action=\".\"><input name=\"new\" type=\"submit\" id=\"new\" value=\"New inquiry\"></form></td></tr></table>";


        include '../dbconfig/closedb.php';
?>
	

<?php getBottom(); ?>